{% extends 'front/page/blog/blog_list_base.html' %}

{% block title %}最新博客{% endblock %}

{% block left %}
<div class="card mb-4">
    <div class="card-header bg-secondary text-white">最新TOP10</div>
    <div class="card-body p-0">
        <div class="list-group list-group-flush" id="hot-posts-container">
        </div>
    </div>
</div>
{% endblock %}

{% block right %}<div id="posts-container"></div>{% endblock %}



{% block script %}
   <script>
   const container = document.getElementById('hot-posts-container');

   function timeAgo(datetimeStr) {
    const postDate = new Date(datetimeStr.replace(/-/g, '/'));
    const now = new Date();
    const diff = Math.floor((now - postDate) / 1000); // 单位：秒

    if (diff < 60) return `${diff} 秒前`;
    if (diff < 3600) return `${Math.floor(diff / 60)} 分钟前`;
    if (diff < 86400) return `${Math.floor(diff / 3600)} 小时前`;
    return `${Math.floor(diff / 86400)} 天前`;
}


Every(() => {
    Fetch('{{ url_for('postsrank10toprecent') }}', 'get', {}, res => {
        if (res.status === 'success') {
            container.innerHTML = '';  // 清空现有内容（如果需要刷新数据）
            res.data.forEach(pack => {
                container.innerHTML += LeftItem1(pack.title, timeAgo(pack.time), post_detail_url.replace('0',pack.pid));
            });
        }
    });
}, 10000);

const postsContainer = document.getElementById('posts-container');
Fetch('{{  url_for('postslistbyrecent') }}','get',{}, res => {
    if (res.status === 'success') {
        res.data.forEach(pack => {
            postsContainer.innerHTML += RightItem(post_detail_url.replace('0',pack.pid), '{{ url_for('static',filename='img/default.png') }}', '#', pack.title, pack.author, pack.time, pack.likes,pack.sort);
        });
    }
});


</script>
{% endblock %}




